<template>
  <!-- 放置弹层组件 -->
  <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @open="MenagerList" @close="closefn">
    <!-- 匿名插槽 -->
    <el-form ref="adddept" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in dataListed"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="addfn">确定</el-button>
      <el-button size="small" @click="closefn">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { AddListAPI, getMenageAPI, updataListAPI } from '@/api/demprments'
import { getUsermenagerAPI } from '@/api/user'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    notedata: {
      type: Object,
      default: () => {}
    },
    dataList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur'] },
          { min: 1, max: 10, message: '部门名称内容长度为1~10', trigger: ['blur'] },
          { validator: this.validatePass, trigger: ['blur'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur'] },
          { min: 1, max: 10, message: '部门编码内容长度为1~10', trigger: ['blur'] },
          { validator: this.validatecode, trigger: ['blur'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur'] },
          { min: 1, max: 100, message: '部门介绍内容长度为1~10', trigger: ['blur'] }
        ]
      },
      dataListed: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '修改部门' : '新增部门'
    }
  },
  created() {
    this.MenagerList()
  },
  methods: {
    /**
     * 数据回显
     */
    async getMenager() {
      const res = await getMenageAPI(this.notedata.id)
      console.log(res)
      this.form = res.data
    },
    closefn() {
      // this.$emit('closefnd')
      this.$emit('update:showDialog', false)
      this.$refs.adddept.resetFields()
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
    },
    // 添加功能
    addfn() {
      this.$refs.adddept.validate(async(flag) => {
        if (flag) {
          if (this.form.id) {
            await updataListAPI(this.form)
            this.$message.success('修改成功')
          } else {
            this.form.pid = this.notedata.id
            const res = await AddListAPI(this.form)
            console.log(res)
            this.$message.success('添加成功')
          }
          this.closefn()
          this.$parent.UserList()
        }
      })
    },
    async MenagerList() {
      const res = await getUsermenagerAPI()
      // console.log(res)
      this.dataListed = res.data
    },
    /**
     * 校验name
     */
    validatePass(rule, value, callback) {
      let children = []
      if (this.form.id) {
        if (this.notedata.name === value) {
          callback()
          return
        } else {
          children = this.dataList.filter(item => item.pid === this.notedata.pid)
          // const flag = children.some(item => item.name === value)
          // flag ? callback(new Error('部门名称不能重复')) : callback()
        }
      } else {
        console.log(this.notedata)
        children = this.notedata.children || []
      }
      const flag = children.some(item => item.name === value)
      flag ? callback(new Error('部门名称不能重复')) : callback()
    },
    /**
     * 校验code
     */
    validatecode(rule, value, callback) {
      if (this.form.id && value === this.notedata.code) {
        callback()
        return
      } else {
        const flag = this.dataList.some(item => item.code === value)
        flag ? callback(new Error('部门编码不能重复')) : callback()
      }
    }
  }
}
</script>
